# This file compares the input routing table against a base file 0.0
import sys, commands, time

if len(sys.argv) != 2:
    print "Usages: ",sys.argv[0], "<i/p topology name, e.g. 4755.r1, it will assume that files 4755.r1.graph and 4755.r1.vid are in the same directory as script>"
    sys.exit()

def compareRoutingTables(basefile, newfile):
    fin_base = open(basefile, 'r')
    baseline = fin_base.readline()
    fin_2 = open(newfile,'r')
    line2 = fin_2.readline()
    totalroutingentries = 0
    notsame = 0
    baseline = fin_base.readline()
    line2 = fin_2.readline()
    while baseline != '' and line2 != '':
        # now extract out all the routing entries and compare them one by one
        # Now read the first node's routing table:
        baserouting_entries = []
        routing_entries = []
        base_routing_entries_count = 0
        routing_entries_count = 0
        while baseline.find("RoutingTable") <0 and baseline != '':
            if baseline.find("[") == 0:
                baserouting_entries.append("")
                base_routing_entries_count = base_routing_entries_count + 1
            baserouting_entries[base_routing_entries_count-1] = baserouting_entries[base_routing_entries_count-1] + baseline.strip()
            baseline = fin_base.readline()
        while line2.find("RoutingTable")<0 and line2 != '':
            if line2.find("[") == 0:
                routing_entries.append("")
                routing_entries_count = routing_entries_count + 1
            routing_entries[routing_entries_count-1] = routing_entries[routing_entries_count-1] + line2.strip()
            line2 = fin_2.readline()
        # now compare the routing tables for the current node:
        totalroutingentries = base_routing_entries_count + totalroutingentries
        for i in range(0, min(len(baserouting_entries), len(routing_entries) )):
            if baserouting_entries[i] != routing_entries[i]:
                #print "--->\tThese 2 lines are different\n", baserouting_entries[i] , '\n------------\n', routing_entries[i]
                notsame = notsame + 1    
        baseline = fin_base.readline()
        line2 = fin_2.readline()
        if line2 == '' or baseline == '':
            break
    fin_2.close()
    fin_base.close()
    
    print 'Total lines : ', totalroutingentries, ' Lines not same: ',notsame
    return [totalroutingentries, notsame]
  
# number of runs per fraction of dropping edges:
nruns = 25

#Number of edges to drop in every iterations:
fracdrop = 0.05
startfrac = 0.05
maxfrac = 0.8
# Now first generate the base routing tables.
print commands.getoutput('java Viro -g '+ sys.argv[1]+'.graph ' + sys.argv[1]+'.vid' )
basefile = sys.argv[1] + '.graph.rt.0.0'
currentfrac = startfrac
outfile = open(sys.argv[1]+'.edge-deletion.measurements', 'w')
outfile.write("<frac edges dropped> <fraction of new entries, space separated>\n")
outfile1 = open(sys.argv[1]+'.edge-deletion.measurements.absolute', 'w')
outfile1.write("<frac edges dropped> <total number of routing entries> <number of new entries, space separated>\n")
while currentfrac < maxfrac:
    run_counter = 0
    newfile = sys.argv[1]+'.graph.rt.'+str(currentfrac)
    outfile.write(str(currentfrac)+' ')
    outfile1.write(str(currentfrac)+' ')    
    print 'Frac: ',currentfrac
    while run_counter < nruns:
        print "Current Run: ", run_counter
        #args1 = ['java','Viro', '-g', '/Users/sourj/Documents/workspace/virojava/'+sys.argv[1]+'.graph ' , '/Users/sourj/Documents/workspace/virojava/'+sys.argv[1]+'.vid' ,' -d',str(currentfrac)]
        #os.execvp('java', args1)
        seed = time.time()
        toexecute = 'java Viro -g '+ sys.argv[1]+'.graph ' + sys.argv[1]+'.vid' + ' -d '+str(currentfrac) + ' '+str(run_counter*10)
        print 'Executing:', toexecute
        commands.getoutput(toexecute)
        
        change = compareRoutingTables (basefile, newfile)
        print 'For this run ', change, ' fraction: ', change[1]/float(change[0])
        outfile.write(str(float(change[1])/change[0]) + ' ')
        if (run_counter == 0):
            outfile1.write(str(change[0]) + ' ')
        outfile1.write(str(change[1]) + ' ')        
        run_counter = run_counter + 1
    currentfrac = currentfrac + fracdrop
    outfile.write('\n')
    outfile1.write('\n')
outfile.close()
outfile1.close()
